<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/10
 * Time: 16:09
 */
namespace Bstage\Controller;

class TeamExamineItemController extends BstageController {

    /**
     * 设置班子综合考评指标接口
     */
    public function setItemAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $team_examine_data = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("status,update_time")
            ->find();
        if(!$team_examine_data){
            $this->errorReturn("班子考评ID错误");
        }

        if($team_examine_data['status'] != 0){
            $this->errorReturn("该考评无法重新设置");
        }

        $item_json = $_REQUEST['item_json']?$_REQUEST['item_json']:$this->errorReturn("缺少指标json");

        $item_array = json_decode($item_json,true);

        $model = M();
        $model->startTrans();

        M("TeamExamineItem")
            ->where(['team_examine_id'=>$team_examine_id])
            ->delete();

        $item_num = 1;
        foreach($item_array as $key=>$value){
            //team_examine_id,item_num,title,item_name
            $new_data = array();
            $new_data['team_examine_id'] = $team_examine_id;
            $new_data['title'] = $value['title'];

            $subitem_array = $value['item'];
            for($i=0;$i<count($subitem_array);$i++){
                $new_data['item_num'] = $item_num;
                $new_data['item_name'] = $subitem_array[$i];
                $add_result = M("TeamExamineItem")
                    ->add($new_data);
                if(!$add_result){
                    $model->rollback();
                    $this->errorReturn("添加失败，请重新尝试");
                }
                $item_num++;
            }
        }

        $update_array = array();
        $update_array['item_json'] = $item_json;
        $update_array['update_time'] = $team_examine_data['update_time'] + 1;
        $update_examine = M("TeamExamine")
            ->where(['team_examine_id'=>$team_examine_id])
            ->save($update_array);
        if(!$update_examine){
            $model->rollback();
            $this->errorReturn("保存指标json失败，请重新尝试");
        }

        $model->commit();
        $this->successReturn("保存成功");
    }


    /**
     * 获取班子综合考评指标列表接口
     */
    private function getItemList(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");

        $item_list = M("TeamExamineItem")
            ->where(['team_examine_id'=>$team_examine_id])
            ->field("item_id,title,item_json")
            ->select();
        if(!$item_list){
            $this->returnJson(503,"无数据");
        }

        foreach($item_list as $key=>$value){
            $item_array = json_decode($value['item_json'],true);
            $item_str = implode(",",$item_array);
            $item_list[$key]['item_str'] = $item_str;
            unset($item_list[$key]['item_json']);
        }

        $this->successReturn("获取成功",$item_list);
    }

    /**
     * 添加班子综合考评指标接口
     */
    private function addItemAction(){
        $new_data = array();
        //team_examine_id,title,item_json
        $new_data['team_examine_id'] = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("缺少指标类型名称");
        $item_str = $_REQUEST['item_str']?$_REQUEST['item_str']:$this->errorReturn("缺少指标数据");
        $item_array = $id_array = explode(",",$item_str);
        if(!$item_array){
            $this->errorReturn("缺少指标数据");
        }
        $new_data['item_json'] = json_encode($item_array);

        $add_result = M("TeamExamineItem")
            ->add($new_data);
        if(!$add_result){
            $this->errorReturn("添加考核指标失败");
        }
        $this->successReturn("添加成功",['item_id'=>$add_result]);
    }

    /**
     * 修改班子综合考评指标接口
     */
    private function editItemAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $item_id = $_REQUEST['item_id']?$_REQUEST['item_id']:$this->errorReturn("缺少班子综合考评指标ID");

        //title,item_json
        $new_data = array();
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("缺少指标类型名称");
        $item_str = $_REQUEST['item_str']?$_REQUEST['item_str']:$this->errorReturn("缺少指标数据");
        $item_array = $id_array = explode(",",$item_str);
        if(!$item_array){
            $this->errorReturn("缺少指标数据");
        }
        $new_data['item_json'] = json_encode($item_array);

        $update_result = M("TeamExamineItem")
            ->where(['item_id'=>$item_id,'team_examine_id'=>$team_examine_id])
            ->save($new_data);
        if(!$update_result){
            $this->errorReturn("修改失败");
        }
        $this->successReturn("修改成功");
    }

    /**
     * 删除班子综合考评指标接口
     */
    private function deleteItemAction(){
        $team_examine_id = $_REQUEST['team_examine_id']?$_REQUEST['team_examine_id']:$this->errorReturn("缺少班子考评ID");
        $id_str = $_REQUEST['id_str']?$_REQUEST['id_str']:$this->errorReturn("缺少班子综合考评指标ID");

        $delete_result = M("TeamExamineItem")
            ->where(['team_examine_id'=>$team_examine_id,'item_id'=>['in',$id_str]])
            ->delete();
        if(!$delete_result){
            $this->errorReturn("删除失败，请重新尝试");
        }
        $this->successReturn("删除成功");
    }

}